package mypackage;

import java.io.IOException;
import javax.microedition.media.Control;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.protocol.ContentDescriptor;
import javax.microedition.media.protocol.DataSource;
import javax.microedition.media.protocol.SourceStream;
import net.rim.device.api.media.control.AudioPathControl;

public class PlayerClient extends Thread implements PlayerListener
{
    private Player playerP;

    private long mStartTime = 0;

    private SourceStream sourceStram = new SourceStream()
    {
        ContentDescriptor mContentDescriptor = new ContentDescriptor(
            "audio/pcm");

        public Control[] getControls()
        {
            return null;
        }

        public Control getControl(String controlType)
        {
            return null;
        }

        public long tell()
        {
            if (mStartTime == 0)
            {
                return 0;
            }
            return(System.currentTimeMillis() - mStartTime);
        }

        public long seek(long where) throws IOException
        {
            return where;
        }

        public int read(byte[] b, int off, int len) throws IOException
        {
            MyScreen.log("Play client read" + " offset:" + off + " length:"
                + len);
            if (mStartTime == 0)
            {
                mStartTime = System.currentTimeMillis();
            }
            if (len > 1024)
            {
                System.arraycopy(new byte[1024], 0, b, off, 1024);
                return 1024;

            }
            else
            {
                System.arraycopy(new byte[len], 0, b, off, len);
                return len;
            }

        }

        public int getTransferSize()
        {
            return 0;
        }

        public int getSeekType()
        {
            return SEEKABLE_TO_START;
        }

        public long getContentLength()
        {
            //return Integer.MAX_VALUE;
            return 100*1024;
        }

        public ContentDescriptor getContentDescriptor()
        {
            return mContentDescriptor;
        }
    };

    public void run()
    {
        try
        {
            playerP = Manager.createPlayer(new DataSource(null)
            {
                SourceStream[] mStream =
                {sourceStram};

                public void stop() throws IOException
                {

                }

                public void start() throws IOException
                {

                }

                public SourceStream[] getStreams()
                {
                    return mStream;
                }

                public String getContentType()
                {
                    return "audio/x-pcm";
                }

                public void disconnect()
                {

                }

                public void connect() throws IOException
                {

                }

                public Control[] getControls()
                {
                    return null;
                }

                public Control getControl(String controlType)
                {
                    return null;
                }
            });
            playerP.addPlayerListener(this);
            playerP.realize();
            AudioPathControl lPathCtr = (AudioPathControl) playerP
                .getControl("net.rim.device.api.media.control.AudioPathControl");
            lPathCtr.setAudioPath(AudioPathControl.AUDIO_PATH_HANDSFREE);

            playerP.prefetch();

            playerP.start();
        }
        catch (IOException e)
        {
            MyScreen.log("Creat player IOException:" + e.toString());
            e.printStackTrace();
        }
        catch (MediaException e)
        {
            MyScreen.log("Creat player MediaException:" + e.toString());
            e.printStackTrace();
        }
    }

    public void playerUpdate(Player player, String event, Object eventData)
    {
        MyScreen.log("player event:" + event);
        if (event == PlayerListener.BUFFERING_STARTED)
        {
            MyScreen.log("player event BUFFERING_STARTED");
        }
        else if (event == PlayerListener.BUFFERING_STOPPED)
        {
            MyScreen.log("player event BUFFERING_STOPPED");
        }
        else if (event == PlayerListener.ERROR)
        {
            MyScreen.log("player event ERROR " + " errorCode:" + eventData);
        }
        else if (event == PlayerListener.STARTED)
        {
            MyScreen.log("player event STARTED");
        }
        else if (event == PlayerListener.STOPPED)
        {
            MyScreen.log("player event STOPPED");
        }
        else if (event == PlayerListener.END_OF_MEDIA)
        {
            MyScreen.log("player event END_OF_MEDIA" + " media time:"
                + eventData);
        }
    }
}
